/**
 * Copyright (c) Tiny Technologies, Inc. All rights reserved.
 * Licensed under the LGPL or a commercial license.
 * For LGPL see License.txt in the project root for license information.
 * For commercial licenses see https://www.tiny.cloud/
 *
 * Version: 5.8.2 (2021-06-23)
 */
(function () {
  'use strict';
  var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  var addMenuButton = function (editor, name, styles) {
    editor.ui.registry.addMenuButton('addVariable', {
      tooltip: name,
      text: name,
      fetch: function (callback) {
        var items = global$1.map(styles, function (styleValue) {
          return {
            type: 'menuitem',
            value: styleValue,
            text: styleValue,
            onAction: function () {
              var value = styleValue.trim();
              var formulaDom = editor.$(".formula-param");
              var index = -1
              for (var i = 0; formulaDom && i < formulaDom.length; i++) {
                if (formulaDom[i].getAttribute("data-content") == value) {
                  index = i;
                }
              }
              if (index === -1) {
                editor.insertContent(
                  '&nbsp;<span class="formula-param" data-content="' + value + '" contenteditable="false">' + value + '*</span>&nbsp;'
                );
              }
            }
          };
        });
        callback(items);
      },
    });
  };

  function Plugin() {
    global.add('addVariable', function (editor) {
      if (editor.hasPlugin('lists')) {
        addMenuButton(editor, "添加随机变量", ['a', 'b ', 'c', 'd', 'e']);
      } else {
        console.error('Please use the Lists plugin together with the Advanced List plugin.');
      }
    });
  }

  Plugin();

}());